/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * createRentableItemPanel.java
 *
 * Created on 09-06-2010, 09:02:45
 */

package gruppe1projekt.GUILayer.rent;

import gruppe1projekt.ControllerLayer.RentController;
import gruppe1projekt.GUILayer.BaseWindow;
import gruppe1projekt.ModelLayer.RentableItem;
import javax.swing.JOptionPane;

/**
 *
 * @author Evil-j
 */
public class CreateRentableItemPanel extends BaseWindow {

    /** Creates new form createRentableItemPanel */
    public CreateRentableItemPanel() {
        initComponents();

        this.RequiresLogin = true;
    }

    @Override
    public boolean IsValid()
    {
        if(txtType.getText().equals(""))
        {
            lblTypeValidate.setText("Du skal skrive noget i denne tekstboks");
            return false;
        }

        if(txtDescription.getText().equals(""))
        {
            return false;
        }

        if(!isDouble(txtDayPrice.getText()))
        {
            return false;
        }

        if(!isLong(txtSerialNumber.getText()))
        {
            return false;
        }

        RentController ctr = new RentController();
        RentableItem item = ctr.readRentableItem(Long.parseLong(txtSerialNumber.getText()));

        if(item != null)
        {
            return false;
        }

        return true;
    }

    


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabelType = new javax.swing.JLabel();
        jLabelDescription = new javax.swing.JLabel();
        jLabelDayPrice = new javax.swing.JLabel();
        txtType = new javax.swing.JTextField();
        DeleteButton = new javax.swing.JButton();
        CreateButton = new javax.swing.JButton();
        txtSerialNumber = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescription = new javax.swing.JTextArea();
        lblTypeValidate = new javax.swing.JLabel();
        lblDescriptionValidate = new javax.swing.JLabel();
        lblDayPriceValidate = new javax.swing.JLabel();
        lblSerialNumberValidate = new javax.swing.JLabel();
        txtDayPrice = new javax.swing.JTextField();
        jLabelSerialNumber = new javax.swing.JLabel();

        jLabel1.setText("jLabel1");

        setAutoscrolls(true);
        setMaximumSize(new java.awt.Dimension(618, 580));
        setMinimumSize(new java.awt.Dimension(618, 580));
        setPreferredSize(new java.awt.Dimension(618, 580));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Indtast produkt informationer", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Verdana", 1, 14))); // NOI18N
        jPanel1.setMaximumSize(null);

        jLabelType.setText("Type");

        jLabelDescription.setText("Beskrivelse");

        jLabelDayPrice.setText("Dagspris");

        txtType.setFont(new java.awt.Font("Verdana", 0, 12));

        DeleteButton.setText("Slet informationer");
        DeleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteButtonActionPerformed(evt);
            }
        });

        CreateButton.setText("Opret produkt");
        CreateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CreateButtonActionPerformed(evt);
            }
        });

        txtSerialNumber.setFont(new java.awt.Font("Verdana", 0, 12));
        txtSerialNumber.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSerialNumberActionPerformed(evt);
            }
        });

        txtDescription.setColumns(20);
        txtDescription.setLineWrap(true);
        txtDescription.setRows(5);
        jScrollPane1.setViewportView(txtDescription);

        lblTypeValidate.setForeground(new java.awt.Color(255, 0, 0));

        lblDescriptionValidate.setForeground(new java.awt.Color(255, 0, 0));

        lblDayPriceValidate.setForeground(new java.awt.Color(255, 0, 0));

        lblSerialNumberValidate.setForeground(new java.awt.Color(255, 0, 0));

        txtDayPrice.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDayPriceActionPerformed(evt);
            }
        });

        jLabelSerialNumber.setText("Serie nummer");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelDescription)
                    .addComponent(jLabelType)
                    .addComponent(jLabelDayPrice)
                    .addComponent(jLabelSerialNumber))
                .addGap(90, 90, 90)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(CreateButton)
                        .addGap(18, 18, 18)
                        .addComponent(DeleteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblSerialNumberValidate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 289, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblDayPriceValidate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 289, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(txtSerialNumber, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                        .addComponent(txtDayPrice, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                        .addComponent(txtType, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                        .addComponent(lblTypeValidate, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblDescriptionValidate, javax.swing.GroupLayout.Alignment.LEADING)))
                .addGap(132, 132, 132))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(63, 63, 63)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelType))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblTypeValidate)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelDescription))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblDescriptionValidate)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDayPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelDayPrice))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblDayPriceValidate)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSerialNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelSerialNumber))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblSerialNumberValidate)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CreateButton)
                    .addComponent(DeleteButton))
                .addContainerGap(120, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void CreateButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_CreateButtonActionPerformed
    {//GEN-HEADEREND:event_CreateButtonActionPerformed
        if(IsValid())
        {
            String type = txtType.getText();
            String description = txtDescription.getText();
            double dayPrice = Double.parseDouble(txtDayPrice.getText());
            long serialNumber = Long.parseLong(txtSerialNumber.getText());

            ClearActionPerformed(evt);

            RentController ctr = new RentController();
            ctr.createRentableItem(type, description, dayPrice, serialNumber);
            JOptionPane.showMessageDialog(this, "Udlejnings produktet er blevet oprettet i systemet");
        }
        else
        {
            JOptionPane.showMessageDialog(this, "Udlejnings produktet er ikke blevet oprettet, /n " +
                    "da der er nogle informationer der er forkerte");
        }
        

}//GEN-LAST:event_CreateButtonActionPerformed

    private void DeleteButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_DeleteButtonActionPerformed
    {//GEN-HEADEREND:event_DeleteButtonActionPerformed
        ClearActionPerformed(evt);
}//GEN-LAST:event_DeleteButtonActionPerformed

    private void txtSerialNumberActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_txtSerialNumberActionPerformed
    {//GEN-HEADEREND:event_txtSerialNumberActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtSerialNumberActionPerformed

    private void txtDayPriceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDayPriceActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtDayPriceActionPerformed

    private void ClearActionPerformed(java.awt.event.ActionEvent evt)
    {
        txtType.setText("");
        txtDescription.setText("");
        txtDayPrice.setText("");
        txtSerialNumber.setText("");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CreateButton;
    private javax.swing.JButton DeleteButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelDayPrice;
    private javax.swing.JLabel jLabelDescription;
    private javax.swing.JLabel jLabelSerialNumber;
    private javax.swing.JLabel jLabelType;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblDayPriceValidate;
    private javax.swing.JLabel lblDescriptionValidate;
    private javax.swing.JLabel lblSerialNumberValidate;
    private javax.swing.JLabel lblTypeValidate;
    private javax.swing.JTextField txtDayPrice;
    private javax.swing.JTextArea txtDescription;
    private javax.swing.JTextField txtSerialNumber;
    private javax.swing.JTextField txtType;
    // End of variables declaration//GEN-END:variables

}
